package com.smartwebx.ha.biz.rainwater.feature.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.common.base.Function;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableListMultimap;
import com.google.common.collect.Multimaps;
import com.smartwebx.ha.biz.rainwater.feature.FeatureRainfallManager;
import com.smartwebx.ha.dal.feature.dao.FeatureRainfallMapper;
import com.smartwebx.ha.dal.usebean.DateValue;
import com.smartwebx.ha.dal.usebean.FeatureRainSlideDay;
import com.smartwebx.ha.dal.usebean.FeatureRainfallYear;
import com.smartwebx.ha.dal.usebean.enu.FeatureRainfallEnum;

@Service
public class FeatureRainfallManagerImpl implements FeatureRainfallManager {
	@Autowired
	private FeatureRainfallMapper featureRainfallDao;

	@Override
	public FeatureRainSlideDay getRainSlideDay(String stcd) {
		List<FeatureRainfallYear> featureRainfallYears = featureRainfallDao
				.getRainSlideDay(stcd);

		ImmutableListMultimap<String, FeatureRainfallYear> prdFeatureRainfallMap = Multimaps
				.index(featureRainfallYears,
						new Function<FeatureRainfallYear, String>() {
							public String apply(FeatureRainfallYear input) {
								return StringUtils.lowerCase(input.getPrd());
							}
						});

		FeatureRainSlideDay featureRainSlideDay = new FeatureRainSlideDay();

		for (FeatureRainfallEnum featureRainfallEnum : FeatureRainfallEnum
				.values()) {
			String rainFallEnumVal = StringUtils.lowerCase(featureRainfallEnum
					.toString());
			ImmutableList<FeatureRainfallYear> findRainfalls = prdFeatureRainfallMap
					.get(rainFallEnumVal);
			DateValue dv = null;
			if (findRainfalls.isEmpty()) {
				dv = DateValue.getDefDv();
			} else {
				FeatureRainfallYear mxpMax = FeatureRainfallYear.mxpByOrder
						.max(findRainfalls);
				dv = new DateValue();
				dv.setTm(mxpMax.getMxtm());
				dv.setVal(mxpMax.getMxp());
			}
			switch (featureRainfallEnum) {
			case H1:
				featureRainSlideDay.setOneHour(dv);
				break;
			case H3:
				featureRainSlideDay.setThreeHour(dv);
				break;
			case H6:
				featureRainSlideDay.setSixHour(dv);
				break;
			case H12:
				featureRainSlideDay.setTwelveHour(dv);
				break;
			case H24:
				featureRainSlideDay.setTwentyFourHour(dv);
				break;
			case D1:
				featureRainSlideDay.setOneDay(dv);
				break;
			case D3:
				featureRainSlideDay.setThreeDay(dv);
				break;
			case D7:
				featureRainSlideDay.setSevenDay(dv);
				break;
			case D15:
				featureRainSlideDay.setFifteenDay(dv);
				break;
			case D30:
				featureRainSlideDay.setThirtyDay(dv);
				break;
			default:
				break;
			}
		}

		return featureRainSlideDay;
	}
}